package com.mx.project.controller;

import com.mx.project.common.exception.JKException;
import com.mx.project.common.resp.ApiResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

@Log4j2
@RestControllerAdvice
public class VerifyTokenExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public ApiResult<Object> handle(HttpServletResponse response, Exception exception) {
        response.setCharacterEncoding("utf-8");
        if(exception instanceof JKException) {
            JKException jkException = (JKException)exception;
            return new ApiResult<>(jkException.getCode(), jkException.getMessage());
        }else {
            log.error("系统异常",exception);
            return new ApiResult<>(-1, exception.getMessage());
        }
    }
}
